/***************************************************************************
*   Copyright (C) 2010-2011 by swkyer <swkyer@gmail.com>                  *
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License as published by  *
*   the Free Software Foundation; either version 2 of the License, or     *
*   (at your option) any later version.                                   *
*                                                                         *
*   This program is distributed in the hope that it will be useful,       *
*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
*   GNU General Public License for more details.                          *
*                                                                         *
*   You should have received a copy of the GNU General Public License     *
*   along with this program; if not, write to the                         *
*   Free Software Foundation, Inc.,                                       *
*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
***************************************************************************/
#ifndef __BREAKPOINT_H__
#define __BREAKPOINT_H__

#include "Ejtag.h"


#define MIPS32_DEFAULT_MAX_SWBP		64
#define MIPS32_DEFAULT_MAX_HWBP		4
#define MIPS32_DEFAULT_MAX_HWWP		2

// Software breakpoint instruction "SDBPP"
#define MIPS32_SW_BREAKPOINT_INSTRUCTION    0x7000003F


typedef enum __bptype
{
    BP_TYPE_UNKNOW = -1,
    BP_TYPE_SW_BPT = 0,
    BP_TYPE_HW_BPT = 1,
    BP_TYPE_HW_WPT_W = 2,
    BP_TYPE_HW_WPT_R = 3,
    BP_TYPE_HW_WPT_RW = 4,
} bptype_t;

typedef enum __bpstate
{
    BP_STATE_NONE = 0,
    BP_STATE_SET,
    BP_STATE_ACTIVE,
    BP_STATE_REMOVED,
} bpstate_t;

typedef enum __bpupdate
{
    BP_ACTIVE_CLR = 0,
    BP_ACTIVE_SET,
} bpupdate_t;


typedef struct __mips32_bpt
{
    struct __mips32_bpt *next;

    bptype_t type;
    bpstate_t state;
    int32_t refcnt;

    ubase_t addr;
    ubase_t addr_mask;
    ubase_t data_compare;
    ubase_t data_compare_mask;

    ubase_t instruction;
    ubase_t asid;
} mips32_bpt_t;

typedef struct __mips32_bpt_info
{
    void *pmips32;

    mips32_bpt_t *bps;
} mips32_bpt_info_t;


int mips32_bpt_init(mips32_bpt_info_t *pbpyinfo, void *pmips32);
int mips32_bpt_exit(mips32_bpt_info_t *pbpyinfo);
int mips32_bpt_add(mips32_bpt_info_t *pbpyinfo, mips32_bpt_t *pbp);
int mips32_bpt_del(mips32_bpt_info_t *pbpyinfo, mips32_bpt_t *pbp);

int mips32_bpt_add_softbp(mips32_bpt_info_t *pbpyinfo, ubase_t addr);
int mips32_bpt_del_softbp(mips32_bpt_info_t *pbpyinfo, ubase_t addr);
int mips32_bpt_add_hardbp(mips32_bpt_info_t *pbpyinfo, ubase_t addr, ubase_t mask);
int mips32_bpt_del_hardbp(mips32_bpt_info_t *pbpyinfo, ubase_t addr);

int mips32_bpt_update(mips32_bpt_info_t *pbpyinfo, mips32_bpt_t *pbpt, bpupdate_t active);
int mips32_bpts_update(mips32_bpt_info_t *pbpyinfo, bpupdate_t active);


#endif /* end of __BREAKPOINT_H__ */
